package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 进货单保存
 */
@RestController
@RequestMapping("/purchaseListGoods")
public class purchaseListController {

    @Autowired
    private PurchaseListService purchaseListService;
    //保存进货单及进货商品信息
    @PostMapping("/save")
    public ServiceVO<PurchaseList> savePurchaseListGoods(PurchaseList purchaseList,String purchaseListGoodsStr
                                            ,HttpServletRequest request){
        String purchaseNumber = request.getParameter("purchaseNumber");
        ServiceVO<PurchaseList> serviceVo = purchaseListService.savePurchaseListAndGoods(purchaseList, purchaseNumber, purchaseListGoodsStr);

        return serviceVo;
    }
    //查询进货单信息列表
    @PostMapping("list")
    public Map<String,Object> getPurchaseList(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseList> list =  purchaseListService.getPurchaseList(purchaseNumber,supplierId,state,sTime,eTime);
        map.put("rows",list);
        return map;
    }
    //查询进货单商品信息列表
    @PostMapping("goodsList")
    public Map<String,Object> getPurchaseListGoodsList(Integer purchaseListId){
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseListGoods> listGoodsList = purchaseListService.getPurchaseListGoodsList(purchaseListId);
        map.put("rows",listGoodsList);
        return map;
    }
    //进货单删除 并删除进货单对应商品
    @PostMapping("delete")
    public ServiceVO deletePurchaseList(Integer purchaseListId){
        ServiceVO serviceVO = purchaseListService.deletePurchaseList(purchaseListId);
        return serviceVO;
    }
    //1.1、支付结算（修改进货单付款状态）
    @PostMapping("updateState")
    public ServiceVO updateStatePurchaseList(Integer purchaseListId){
        ServiceVO serviceVO = purchaseListService.updateStatePurchaseList(purchaseListId);
        return serviceVO;
    }
    //3.1、进货统计（可根据 商品类别、商品编码或名称 条件查询）
    @PostMapping("count")
    public String countPurchaseListGoodsList(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        String jsonStr = purchaseListService.countPurchaseListGoodsList(sTime,eTime,goodsTypeId,codeOrName);
        return jsonStr;
    }
}
